package com.mingtu.jgspatrol.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.GridSpacingItemDecoration;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.adapter.EcWeatherAdapterNew;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.bean.AirBean;
import com.mingtu.jgspatrol.bean.WeatherBean;
import com.mingtu.jgspatrol.databinding.ActivityEcRealtimeDataNewBinding;
import com.mingtu.jgspatrol.utils.MyConstant;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

public class EcRealtimeDataActivityNew extends MyBaseActivity<ActivityEcRealtimeDataNewBinding> {
    private EcWeatherAdapterNew ecWeatherAdapter;
    private WeatherBean.DataBean weatherBeanData;

    @Override
    protected void initView() {
        setModuleTitle("生态监测");
        super.initView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ec_realtime_data_new;
    }

    @Override
    protected void initData() {
        int spanCount = ScreenUtils.isTablet() ? 3 : 2;
        GridLayoutManager layoutManager = new GridLayoutManager(context, spanCount);//第二个参数为网格的列数
        binding.recycler.addItemDecoration(new GridSpacingItemDecoration(spanCount, SizeUtils.dp2px(5), false));
//        recycler.setHasFixedSize(true);
        binding.recycler.setLayoutManager(layoutManager);

        ecWeatherAdapter = new EcWeatherAdapterNew(context, null);
        binding.recycler.setAdapter(ecWeatherAdapter);
    }


    @Override
    protected void getData() {
        getWeatherData();
        initListener();
    }


    private void initListener() {
        initPullRefresh();//刷新
    }

    private void initPullRefresh() {
        binding.srlUp.setEnableLoadMore(false);
        binding.srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.srlUp.finishRefresh(Constant.REFRESH_TIME);
                binding.srlUp.setNoMoreData(false);
                getWeatherData();
            }
        });
    }


    /**
     * 天气数据
     */
    private void getWeatherData() {
        OkGo.<String>post(MyConstant.POST_WEATHER_NOW)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            WeatherBean weatherBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                weatherBean = new WeatherBean();
                                weatherBean = gson.fromJson(result, WeatherBean.class);
                                weatherBeanData = weatherBean.getData();
                                getAirData();

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                binding.recycler.setVisibility(View.GONE);
                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));

                            }
                        }
                    }
                });
    }

    /**
     * 天气数据
     */
    private void getAirData() {
        OkGo.<String>post(MyConstant.POST_AIR_NOW)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            AirBean airBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                airBean = new AirBean();
                                airBean = gson.fromJson(result, AirBean.class);
                                AirBean.DataBean dataBean = airBean.getData();

                                String api = dataBean.getAqi();
                                String category = dataBean.getCategory();
                                String co = dataBean.getCo();
                                String level = dataBean.getLevel();
                                String no2 = dataBean.getNo2();
                                String o3 = dataBean.getO3();
                                String so2 = dataBean.getSo2();
                                String pm2p5 = dataBean.getPm2p5();
                                String pm10 = dataBean.getPm10();
                                String pubTime = dataBean.getPubTime();


                                weatherBeanData.setAqi(api);
                                weatherBeanData.setCategory(category);
                                weatherBeanData.setCo(co);
                                weatherBeanData.setLevel(level);
                                weatherBeanData.setNo2(no2);
                                weatherBeanData.setO3(o3);
                                weatherBeanData.setSo2(so2);
                                weatherBeanData.setPm2p5(pm2p5);
                                weatherBeanData.setPm10(pm10);
                                weatherBeanData.setPubTime(pubTime);

                                ecWeatherAdapter.upData(weatherBeanData);

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));
                            }

                        }
                    }
                });
    }
}